using System;
using System.IO;
using System.Threading;
using SevenZip;
using SevenZip.Compression.LZMA;
using UnityEngine;
namespace Compress.LZMA
{
    public class LZMAFile
    {
        public class CodeProgress : ICodeProgress
        {
            public ProgressDelegate m_ProgressDelegate = null;
            public CodeProgress(ProgressDelegate del)
            {
                this.m_ProgressDelegate = del;
            }
            public void SetProgress(long inSize, long outSize)
            {
            }
            public void SetProgressPercent(long fileSize, long processSize)
            {
                this.m_ProgressDelegate(fileSize, processSize);
            }
        }
        public static void CompressAsync(FileChangeInfo info)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(LZMAFile.Compress));
            thread.Start(info);
        }
        public static void DeCompressAsync(FileChangeInfo info)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(LZMAFile.DeCompress));
            thread.Start(info);
        }


        public static void CompressAsync(string inpath, string outpath, ProgressDelegate progress)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(LZMAFile.Compress));
            thread.Start(new FileChangeInfo
            {
                inpath = inpath,
                outpath = outpath,
                progressDelegate = progress
            });
        }
        public static void DeCompressAsync(string inpath, string outpath, ProgressDelegate progress)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(LZMAFile.DeCompress));
            thread.Start(new FileChangeInfo
            {
                inpath = inpath,
                outpath = outpath,
                progressDelegate = progress
            });
        }
        private static void Compress(object obj)
        {
            FileChangeInfo fileChangeInfo = (FileChangeInfo)obj;

            try
            {
                string inpath = fileChangeInfo.inpath;
                string outpath = fileChangeInfo.outpath;
                LZMAFile.CodeProgress progress = null;
                if (fileChangeInfo.progressDelegate != null)
                {
                    progress = new LZMAFile.CodeProgress(fileChangeInfo.progressDelegate);
                }
                Encoder encoder = new Encoder();
                FileStream fileStream = new FileStream(inpath, FileMode.Open);
                FileStream fileStream2 = new FileStream(outpath, FileMode.Create);
                encoder.WriteCoderProperties(fileStream2);
                fileStream2.Write(BitConverter.GetBytes(fileStream.Length), 0, 8);
                encoder.Code(fileStream, fileStream2, fileStream.Length, -1L, progress);
                fileStream2.Flush();
                fileStream2.Close();
                fileStream.Close();
                Debug.Log("压缩完毕");
                if (fileChangeInfo.finish != null)
                {
                    fileChangeInfo.finish();
                }
            }
            catch (System.Exception ex)
            {
                if (fileChangeInfo.ex != null)
                {
                    fileChangeInfo.ex(ex);
                }
            }
        }
        public static void Compress(string inpath, string outpath, ProgressDelegate progress)
        {
            LZMAFile.Compress(new FileChangeInfo
            {
                inpath = inpath,
                outpath = outpath,
                progressDelegate = progress
            });
        }
        private static void DeCompress(object obj)
        {
            FileChangeInfo fileChangeInfo = (FileChangeInfo)obj;
            try
            {
                string inpath = fileChangeInfo.inpath;
                string outpath = fileChangeInfo.outpath;
                LZMAFile.CodeProgress progress = null;
                if (fileChangeInfo.progressDelegate != null)
                {
                    progress = new LZMAFile.CodeProgress(fileChangeInfo.progressDelegate);
                }
                Decoder decoder = new Decoder();
                FileStream fileStream = new FileStream(inpath, FileMode.Open);
                FileStream fileStream2 = new FileStream(outpath, FileMode.Create);
                int num = 5;
                byte[] array = new byte[num];
                fileStream.Read(array, 0, array.Length);
                byte[] array2 = new byte[8];
                fileStream.Read(array2, 0, 8);
                long outSize = BitConverter.ToInt64(array2, 0);
                decoder.SetDecoderProperties(array);
                decoder.Code(fileStream, fileStream2, fileStream.Length, outSize, progress);
                fileStream2.Flush();
                fileStream2.Close();
                fileStream.Close();
                Debug.Log("解压完毕");
                if (fileChangeInfo.finish != null)
                {
                    fileChangeInfo.finish();
                }
            }
            catch (System.Exception ex)
            {
                if (fileChangeInfo.ex != null)
                {
                    fileChangeInfo.ex(ex);
                }
            }
        }
        public static void DeCompress(string inpath, string outpath, ProgressDelegate progress)
        {
            LZMAFile.DeCompress(new FileChangeInfo
            {
                inpath = inpath,
                outpath = outpath,
                progressDelegate = progress
            });
        }
    }
}
